/**
 * 
 */
package de.hellinger.yacc.db.connection;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

/**
 * @author CHH
 * See also:http://stackoverflow.com/questions/7986750/create-session-factory-in-hibernate-4
 */
public class HibernateUtil {

	public static final String CONFIG_PACKAGE_TABLES = "de/hellinger/yacc/bo";
	
	private static final SessionFactory sessionFactory;
	private static final ServiceRegistry serviceRegistry;

	static {

		try {			
			 Configuration hibConfiguration = new Configuration().addPackage(CONFIG_PACKAGE_TABLES).configure();       

			 serviceRegistry = new ServiceRegistryBuilder()
			             .applySettings(hibConfiguration.getProperties())
			             .buildServiceRegistry();

			 sessionFactory = hibConfiguration.buildSessionFactory(serviceRegistry);
		} catch (Throwable ex) {
			System.err.println("Initial SessionFactory creation failed." + ex);
			throw new ExceptionInInitializerError(ex);

		}
	}

	public static SessionFactory getSessionFactory() {
		return sessionFactory;
	}
}